package cn.luyou.admin.controller;

import cn.luyou.admin.common.Result;
import cn.luyou.admin.dto.GeneratePaperDTO;
import cn.luyou.admin.entity.ExamPaper;
import cn.luyou.admin.entity.Question;
import cn.luyou.admin.service.ExamPaperService;
import cn.luyou.admin.utils.BaseContext;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/exam-papers")
public class ExamPaperController {

    @Autowired
    private ExamPaperService examPaperService;

    @GetMapping
    public Result<IPage<ExamPaper>> getPapers(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String keyword) {
        return Result.success(examPaperService.getPapers(pageNum, pageSize, keyword));
    }

    @PostMapping("/generate")
    public Result<ExamPaper> generatePaper(@RequestBody @Valid GeneratePaperDTO generatePaperDTO, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        return Result.success(examPaperService.generatePaper(generatePaperDTO, BaseContext.getCurrentId()));
    }

    @PutMapping("/{id}")
    public Result<Void> updatePaper(@PathVariable Long id, @RequestBody @Valid ExamPaper paper) {
        paper.setId(id);
        examPaperService.updatePaper(paper);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<Void> deletePaper(@PathVariable Long id) {
        examPaperService.deletePaper(id);
        return Result.success();
    }

    @PutMapping("/{id}/status")
    public Result<Void> updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        examPaperService.updateStatus(id, status);
        return Result.success();
    }

    @GetMapping("/{id}/questions")
    public Result<List<Question>> getPaperQuestions(@PathVariable Long id) {
        return Result.success(examPaperService.getPaperQuestions(id));
    }
} 